<?php
declare (strict_types = 1);

namespace app\mini\controller;

use app\crm\model\Setting;
use app\admin\model\Slideshow;
use think\response\Json;
use app\mini\model\SysModelMini;
use app\crm\model\Customer as CustomerModel;
use app\mini\model\PersonCustomer;
use app\crm\model\Setting as SettingModel;
use app\mini\model\PersonCase as PersonCaseModel;
use app\crm\model\ReturnVist;
use app\admin\model\File as FileModel;
use app\admin\model\CustomerModel as AdminCustomerModel;
use app\admin\model\ShopOrderModel as AdminShopOrderModel;
use app\admin\model\PersonModel;

use think\facade\Db;
/**
 * Class Index
 * @package app\mini\controller
 * @author Mr.Liu Wuchen
 * @create 2021-07-02 10:42
 */
class Index extends Access
{

    /**
     * 小程序基础配置
     * @param  Setting $setting
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-07-02 10:48
     */
    public function index(): Json
    {
       $res = Db::name('mini_index')->where('id','=',1)->find();
        return resultArray(['error' => '获取成功', 'data' => $res['wx_index']??'']);
    }
    /**
     * 小程序基础配置
     * @param  Setting $setting
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-07-02 10:48
     */
    public function basis(Setting $setting): Json
    {
        $request['group'] = 'min_basis';
        $request['name']  = ['user_default_logo', 'login_logo', 'customer_service_phone'];
        $data = $setting->getDataInfo($request);
        if (!$data) {
            return resultArray(['error' => $setting->getError()]);
        }
        return resultArray(['error' => $setting->getError(), 'data' => $data]);
    }

    /**
     * 小程序轮播图
     * @param  Slideshow $slideshow
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-07-02 10:48
     */
    public function slideshow(Slideshow $slideshow): Json
    {
        $data = $slideshow->getDataInfo([]);
        if (!$data) {
            return resultArray(['error' => $slideshow->getError()]);
        }
        return resultArray(['error' => $slideshow->getError(), 'data' => $data]);
    }

    /**
     * 获取首页业绩数据
     */
    public function indexshow(): Json
    {
        $request = $this->request->param();

        $request['time'] = '选择时间';

        $model = new SysModelMini();

        // $data = $model->getAdminSysHomeInfoResource($newShopIds,$request);
        // echo "<pre>";
        // print_r($request);
        // echo "</pre>";
        // exit;

        $data = $model->getAdminSysHomeInfoResource($request);

        return resultArray(['error' => '获取成功', 'data' => $data]);
    }

    /**
     * 获取首页客户数据
     */
    public function homecustomer(): Json
    {
        $request = $this->request->param();

        $newShopIds = [];

        if(is_array($request['shop_id'])){

            $newShopIds = $request['shop_id'];

        }else{

            $shopIds = json_decode($request['shop_id'],true) ?? array();

            if(is_array($shopIds)){

                foreach ($shopIds as $key => $value) {

                    array_push($newShopIds,$value);

                }
            }else{

                array_push($newShopIds,$shopIds);

            }

        }

        $model = new SysModelMini();

        $data = $model->getHomeCustomer($newShopIds,$request);
        // $data = ['content' => '获取首页客户数据'];

        return resultArray(['error' => '获取成功', 'data' => $data]);
    }

    /**
     * 获取首页保有数据
     */
    public function homebaoyou(): Json
    {
        $request = $this->request->param();

        $newShopIds = [];

        if(is_array($request['shop_id'])){

            $newShopIds = $request['shop_id'];

        }else{

            $shopIds = json_decode($request['shop_id'],true) ?? array();

            if(is_array($shopIds)){

                foreach ($shopIds as $key => $value) {

                    array_push($newShopIds,$value);

                }
            }else{

                array_push($newShopIds,$shopIds);

            }

        }

        $model = new SysModelMini();

        $data = $model->getHomeBaoyou($newShopIds,$request);
        // $data = ['content' => '获取首页客户数据'];

        return resultArray(['error' => '获取成功', 'data' => $data]);
    }

    /**
     * 获取客户详情
     */
    public function read(CustomerModel $customerModel):Json
    {
        $request = $this->request->param();
        $request['is_wx']=1;
        $data = $customerModel->getDataInfo($request);
        if (!$data) {
            return resultArray(['error' => $customerModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }

    /**
     * 获取客户详情2
     */
    public function read2():Json
    {
        $request = $this->request->param();

        $model = new PersonCustomer();

        $data = $model->getCustomerInfo($request);

        return resultArray(['data' => $data]);
    }

    /**
     * 获取客户疤痕照片
     */
    public function photo(CustomerModel $customerModel):Json
    {
        $request = $this->request->param();

        $data = $customerModel->albumList($request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 获取客户消费记录
     */
    public function costList():Json
    {
        $request = $this->request->param();

        $model = new SysModelMini();

        $data = $model->costList($request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

     /**
     * 获取客户服务记录
     */
    public function serviceLog():Json
    {
        $request = $this->request->param();

        $model = new SysModelMini();

        $data = $model->serviceLog($request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 新增客户页面getSetting
     */
    public function setting(SettingModel $settingModel):Json
    {
        $request = $this->request->param();

        $data = $settingModel->getDataList($request);

        if (!$data) {
            return resultArray(['error' => $settingModel->getError()]);
        }

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 新增客户页面addCustomer
     */
    public function addCustomer(CustomerModel $customerModel):Json
    {
        $request = $this->request->param();
        // echo "<pre>";
        // print_r($request);
        // echo "</pre>";
        // exit;

        $data = $customerModel->addData($request);

        if (!$data) {
            return resultArray(['error' => $customerModel->getError()]);
        }

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 客户管理
     */
    public function getDataList():Json
    {
        $request = $this->request->param();

        $model = new SysModelMini();

        $data = $model->getDataList($request);

        if (!$data) {
            return resultArray(['error' => $customerModel->getError()]);
        }

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 案例列表
     */
    public function getCaseList():Json
    {
        $request = $this->request->param();

        $model = new PersonCaseModel();

        $data = $model->getCaseList($request);

        // if (!$data) {
        //     return resultArray(['error' => $customerModel->getError()]);
        // }

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 删除案例
     */
    public function deleteCase():Json
    {
        $request = $this->request->param();

        $model = new PersonCaseModel();

        $data = $model->delCase($request);

        // if (!$data) {
        //     return resultArray(['error' => $customerModel->getError()]);
        // }

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 案例详情
     */
    public function caseData():Json
    {
        $request = $this->request->param();

        $model = new PersonCaseModel();

        $data = $model->getDetail($request);


        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 添加标签
     */
    public function addTag():Json
    {
        $request = $this->request->param();

        $model = new SysModelMini();

        $data = $model->addTag($request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 获取客户标签
     * by yp 2022-09-29
     */
    public function getTag():Json
    {
        $request = $this->request->param();

        $model = new SysModelMini();

        $data = $model->getTag($request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 删除客户标签
     * by yp 2022-09-29
     */
    public function deleteTag():Json
    {
        $request = $this->request->param();

        $model = new SysModelMini();

        $data = $model->deleteTag($request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 获取客户回访信息
     */
    public function getVisit():Json
    {
        $request = $this->request->param();

        $ReturnVist = new ReturnVist();

        $data = $ReturnVist->getDataList($request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 新增客户回访信息
     */
    public function addVisit():Json
    {
        $request = $this->request->param();

        $ReturnVist = new ReturnVist();

        $data = $ReturnVist->addData($request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 新增优案获取顾客列表
     */
    public function getCaseCustomer():Json
    {
        $request = $this->request->param();

        $model = new SysModelMini();

        $data = $model->getCaseCustomer($request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    // 新增案例（mini端）
    public function addCase():Json
    {
        $request = $this->request->param();

        $personCaseModel = new PersonCaseModel();

        $data = $personCaseModel->addCase($request);


        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    // 申请优秀-满意案例（mini端）
    public function sqCase():Json
    {
        $request = $this->request->param();

        $personCaseModel = new PersonCaseModel();

        $data = $personCaseModel->setTj($request);


        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    // 接收上传信息
    public function uploadImgUrl()
    {
        $request = $this->request->param();

        $files    = $this->request->file('file');

        $fileModel = new FileModel();

        $ext = $files->getOriginalExtension();

        if (in_array($ext, ['xlsx', 'xls', 'csv'])) {

            $res = $fileModel->excelData($files, $request);

        } else {

            $res = $fileModel->createData($files, $request);

        }


        if($res){

            return resultArray(['data' => $res]);

        } else {

            return resultArray(['error' => $fileModel->getError()]);

        }

    }

    /**
     * 添加案例跟进
     */
    public function addCaseLog():Json
    {
        $request = $this->request->param();

        $personCaseModel = new PersonCaseModel();

        $request['ht'] = 1;

        $data = $personCaseModel->addCaseLog($request);

        if (!$data) {
            return resultArray(['error' => $personCaseModel->getError()]);
        }

        return resultArray($data);
    }

    /**
     * 上传图片
     */
    public function addAlbum(CustomerModel $CustomerModel):Json
    {
        $request = $this->request->param();

        $request['src'] = json_decode($request['src'],true);

        // echo "<pre>";
        // print_r($request);
        // echo "</pre>";

        // exit;

        $data = $CustomerModel->addAlbum($request);
        if (!$data) {
            return resultArray(['error' => $CustomerModel->getError()]);
        }
        return resultArray(['data' => [],'error' => $CustomerModel->getError()]);
    }

    /**
     * 数据-获取经营数据
     */
    public function getShopStistics():Json
    {
        $request = $this->request->param();

        $model = new SysModelMini();

        $data = $model->getShopStistics($request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 数据-获取经营数据-新单业绩
     */
    public function getShopStistics1():Json
    {
        $request = $this->request->param();

        $model = new SysModelMini();

        $data = $model->getShopStistics1($request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 数据-获取经营数据-补单业绩
     */
    public function getShopStistics2():Json
    {
        $request = $this->request->param();

        $model = new SysModelMini();

        $data = $model->getShopStistics2($request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 数据-获取经营数据-实收业绩
     */
    public function getShopStistics3():Json
    {
        $request = $this->request->param();

        $model = new SysModelMini();

        $data = $model->getShopStistics3($request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 数据-获取经营数据-转诊业绩
     */
    public function getShopStistics4():Json
    {
        $request = $this->request->param();

        $model = new SysModelMini();

        $data = $model->getShopStistics4($request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 数据-获取经营数据-疤痕业绩
     */
    public function getShopStistics5():Json
    {
        $request = $this->request->param();

        $model = new SysModelMini();

        $data = $model->getShopStistics5($request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 数据-获取经营数据-项目业绩
     */
    public function getShopStistics6():Json
    {
        $request = $this->request->param();

        $model = new SysModelMini();

        $data = $model->getShopStistics6($request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 数据-获取客户分析
     */
    public function getCustomerStistics():Json
    {
        $request = $this->request->param();

        $model = new SysModelMini();

        $data = $model->getCustomerStistics($request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 数据-获取员工数据
     */
    public function getStaffStistics():Json
    {
        $request = $this->request->param();

        $model = new SysModelMini();

        $data = $model->getStaffStistics($request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 获取门店护理师列表
     */
    public function getStaffList():Json
    {
        $request = $this->request->param();

        $where = [];

        if($request['position_id'] == 9){

            array_push($where,['sp.id','=',$request['shop_person_id']]);

        }

        $staff_list = Db::name('shop_person')->alias('sp')

            ->join('admin_user au', 'au.shop_person_id = sp.id', 'LEFT')

            ->where('au.is_status',0)

            ->where('sp.shop_id',$request['shop_id'])

            ->where('sp.position_id','in',[7,8,9])

            ->where($where)

            ->column('sp.id');

        return resultArray(['data' => $staff_list,'error'=>'获取成功']);
    }

    /**
     *  客户-是否展示问卷调查接口
     */
    public function isQuestion():Json
    {
        $request = $this->request->param();

        $model = new SysModelMini();

        $data = $model->isQuestion($request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     *  获取满意度调查列表
     *
     */
    public function getQuestionnaire():Json
    {
        $request = $this->request->param();

        $model = new SysModelMini();

        $data = $model->getQuestionnaire($request);

        return resultArray(['data' => $data,'error'=>'获取成功']);

    }

    /**
     * 获取员工小程序-我的预约-列表
     *
     */
    public function getStaffBookList():Json
    {
        $request = $this->request->param();

        $model = new SysModelMini();

        $data = $model->getStaffBookList($request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    /**
     * 获取门店进店（疤痕痘痘项目）
     *
     */
    public function getComeInNum():Json
    {
        $request = $this->request->param();

        $request['pc'] = 1;

        $model = new AdminCustomerModel();

        $data = $model->getCustomerCommonDataCollect($request['shop_id'],$request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

     /**
     * 获取门店成交（疤痕痘痘项目）
     *
     */
    public function getCJNum():Json
    {
        $request = $this->request->param();

        $request['pc'] = 1;

        $model = new AdminShopOrderModel();

        $data = $model->getShopOrderCommonDataCollect($request['shop_id'],$request);

        return resultArray(['data' => $data,'error'=>'获取成功']);
    }

    public function readAdminShopPersonList():Json
    {
        $request = $this->request->param();

        $model = new PersonModel();

        // $shopIds = $this->getCommonShopIdsCollectByUserType();

        // if (empty($request['page']) || empty($request['limit'])) return $this->failed(ErrorCode::SYSTEM_INVALID);

        $res          = $model->getPersonAdminListResource([],(int)$request['page'],(int)$request['limit'],$request);

        return $this->success($res,'获取资源成功',$res['dataCount']);
    }
    /**
     * 获取会员标签选项列表
     *
     * @return mixed
     */
    public function getLabelList()
    {
        $model = new Setting();
        $result = $model->read_member_label([]);
        foreach ($result['list'] as $key =>$value)
        {
            $result['list'][$key]['text']=$value['name'];
            foreach ($value['children'] as $k => $v)
            {
                $result['list'][$key]['children'][$k]['text']=$v['name'];
            }
        }
        return $this->success($result);
    }
    /**
     * 编辑
     * @param  CustomerModel $customerModel
     * @return Json
     */
    public function update(CustomerModel $customerModel): Json
    {
//        $this->param['user_id'] = $this->userInfo['id'];
        $request = $this->request->param();
        $data = $customerModel->saveData($request);
        if (!$data) {
            return resultArray(['error' => $customerModel->getError()]);
        }
        return resultArray(['error' => $customerModel->getError(), 'data'=>[]]);
    }

    /**
     * 获取客户门诊id
     * @param  CustomerModel $customerModel
     * @return Json
     */
    public function readCustomerId(CustomerModel $customerModel): Json
    {
        $request = $this->request->param();

        $telephone = Db::name('customer')->where('id',$request['id'])->value('telephone');

        $ids = Db::name('customer')->alias('c')
        ->join('shop s', 's.shop_id = c.shop_id', 'LEFT')
        ->where('c.telephone',$telephone)->column('c.id','s.shop_type');

        
        return resultArray(['data' => $ids]);
    }

    /**
     * 获取门诊核销护石操作列表
     */
    function getShopPersonControls()
    {
        $controls = Db::name('shop_person_controls')->select()->toArray();
        // $controls = Db::name('shop_person_controls')->column('id,name','id');

        return resultArray(['data' => $controls]);

        // echo "<pre>";
        // print_r($controls);
        // echo "</pre>";
        // exit;
    }
}
